// store/index.ts
import { defineStore } from 'pinia';

interface User {
  id: string;
  name: string;
}

interface Message {
  id: string;
  content: string;
  timestamp: string;
}

interface State {
  db: any,
  count: number;
  user: User;
  messages: Message[];
  latestData: {}
}

export const useStore = defineStore('main', {
  state: (): State => ({
    db: null as any,  // 数据库实例
    count: 0,
    user: {
      id: '',
      name: '',
    },
    messages: [], // 消息列表
    latestData: {}
  }),

  getters: {
    getCount(state: State): number {
      return state.count;
    },
    getUserName(state: State): string {
      return state.user.name;
    },
    getMessages(state: State): Message[] {
      return state.messages;
    },
  },

  actions: {
    setDb(db: any) {
      this.db = db;
    },

    // 通知消息储存到本地数据库
    addNotificationMessage(message: any) {
      console.log(message)

    },
    // 向界面推送所有类型的最新的消息
    updateLatestData(message: any) {
      this.latestData = message;
    },
    // 通知类型储存数据库
    updateNotificationDatabase(message: any) {


    },

    increment() {
      this.count++;
    },
    async fetchUser(userId: string) {
      const user = await new Promise<User>((resolve) =>
        setTimeout(() => resolve({ id: userId, name: 'John Doe' }), 1000)
      );
      this.user = user;
    },
  },
});
